const Koa = require('koa'),
    router = require('koa-router')(),
    static = require('koa-static'),
    path = require('path'),
    onerror = require('koa-onerror'),
    logger = require('koa-logger'),
    json = require('koa-json'),
    cors = require('koa2-cors');

const app = new Koa();
// 监听错误
onerror(app);
// 开放公共文件夹
app.use(static(__dirname+'public'));
app.use(require('koa-bodyparser')());
app.use(cors());
app.use(json());
app.use(logger());

// 配置计算和显示请求url 请求方法 耗时时间 以及文件大小
app.use(function* (next) {
    var start = new Date;
    yield next;
    var ms = new Date - start;
    console.log('%s %s - %s',this.method,this.url,ms);    
});

//配置子路由
const index = require('./routes/index');
router.use('/api/v2',index);

app.on('error', (err, ctx) => {
    console.log('Error-1314:',err,ctx);
});
app.use(router.routes()).use(router.allowedMethods());
app.listen(10002);